<template>
  <!--轮播图-->
  <el-carousel height="550px">
    <el-carousel-item v-for="(banner,index) in banners" :key="index">
      <el-image :src="banner"/>
    </el-carousel-item>
  </el-carousel>

  <!--  广告位-->
  <div class="ad">
    <ul>
      <li>
        <el-image src="/src/assets/banner/3.jpg"/>
      </li>
      <li>
        <el-image src="/src/assets/banner/4.jpg"/>
      </li>
      <li>
        <el-image src="/src/assets/banner/5.jpg"/>
      </li>
      <li>
        <el-image src="/src/assets/banner/2.jpg"/>
      </li>
    </ul>
  </div>

  <!--  推荐的商品-->
  <div class="recom" v-for="(category,index) in parentList" :key="index">
    <div class="categoryName">{{ category.name }}</div>
    <div class="goodsList">
      <ul>
        <li v-for="(goods,index) in category.goodsList" :key="index" @click="toGoodsView(goods.id)">
          <div class="pic">
            <el-image :src="`${SERVER_ADDR}/goods/pic/${goods.picList[0].url}`"/>
          </div>
          <div class="name">{{goods.name}}</div>
          <div class="decp">{{goods.dscp}}</div>
          <div class="price">￥{{goods.price}}</div>
        </li>
      </ul>
    </div>
  </div>
</template>


<script setup>
import {ref} from "vue";
import categoryApi from "@/api/categoryApi.js";
import {useRouter} from "vue-router";
const router=useRouter();
//服务器的地址
const SERVER_ADDR = ref(import.meta.env.VITE_SERVER_ADDR)
const banners = ref([
  "/src/assets/banner/20235102832.jpg",
  "/src/assets/banner/2024314122.jpg",
  "/src/assets/banner/20235106180.jpg",
  "/src/assets/banner/20235107241.jpg",
  "/src/assets/banner/20235107214.jpg",
])
//已经上架父分类
const parentList = ref([])

//1.获取父分类--2.上架
function getParent() {
  const condition = {
    parentId: 0,
    status: 1
  }
  categoryApi.selectByPage(condition)
      .then(resp => {
        console.log(resp.data)
        parentList.value = resp.data
      })
}
//点击实现页面跳转
function toGoodsView(id){
  router.push({
    path:'/user/goods',
    query:{
      id:id
    }
  })
}
getParent()
</script>


<style scoped>
.ad {
  margin: 20px 0;
}

.ad ul li {
  float: left;
  width: 285px;
  margin-right: 20px;
}

.ad ul li:nth-child(4n) {
  margin-right: 0;
}

.recom {
  margin-bottom: 20px;
}

.recom .goodsList ul li {
  width: 224px;
  float: left;
  margin-right: 20px;
  margin-bottom: 20px;
  text-align: center;
  background-color: #eee;
  cursor: pointer;
}

.recom .goodsList ul li:hover {
  box-shadow: 0 0 20px #33b4e3;
}

.recom .goodsList ul li:nth-child(5n) {
  margin-right: 0;
}

.recom .categoryName {
  font-size: 20px;
  margin-bottom: 5px;
  font-weight: bold;
}

.name .desp .price {
  line-height: 25px;
}

.recom .name {
  font-size: 16px;
  font-weight: bold;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.recom .decp {
  color: #AAA;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.recom .price {
  color: var(--theme-color)
}
</style>
